package cn.microven.mindmap.controller.menu;

import cn.microven.mindmap.components.node.AbstractMapNode;
import cn.microven.mindmap.oprations.ChangeNodeBorder;
import cn.microven.mindmap.oprations.ChangeNodeColor;
import cn.microven.mindmap.oprations.ChangeNodeRadius;
import cn.microven.mindmap.struture.MindMap;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;

import java.net.URL;
import java.util.ResourceBundle;

public class NodeStyleMenu implements Initializable {

    @FXML
    public ColorPicker backgroundColor;

    @FXML
    public Slider radiusSlider;

    @FXML
    public TextField radiusInput;

    @FXML
    public ColorPicker borderColor;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        backgroundColor.valueProperty().addListener(((observableValue, color, t1) -> {
            AbstractMapNode node = MindMap.getInstance().getSelectedNode();
            ChangeNodeColor cnc = new ChangeNodeColor(node.getBackgroundColor(),t1,node);
            cnc.start();
        }));
        radiusSlider.valueProperty().addListener(((observableValue, number, t1) -> {
            radiusInput.setText(String.valueOf(t1.intValue()));
            ChangeNodeRadius cnr = new ChangeNodeRadius(MindMap.getInstance().getSelectedNode().getRadius(),(Double)t1,MindMap.getInstance().getSelectedNode());
            cnr.start();
        }));

        radiusInput.textProperty().addListener(((observableValue, s, t1) -> {
            if (t1.length() == 0){
                return;
            }
            try {
                radiusSlider.setValue(Integer.parseInt(t1));
            }
            catch (NumberFormatException e) {
                radiusInput.setText(s);
            }
        }));

        borderColor.valueProperty().addListener(((observableValue, color, t1) -> {
            if (!MindMap.getInstance().isUndoing() && !MindMap.getInstance().isRedoing()) {
                ChangeNodeBorder cnb = new ChangeNodeBorder((Color) MindMap.getInstance().getSelectedNode().getNodeShape().getStroke(),
                        t1, MindMap.getInstance().getSelectedNode());
                cnb.start();
            }
        }));
    }

    public void setBorder() {
        radiusSlider.maxProperty().unbind();
        if (MindMap.getInstance().getSelectedNode().getHeight() == 0){
            radiusSlider.setMax(100);
        }
        else radiusSlider.maxProperty().bind(MindMap.getInstance().getSelectedNode().heightProperty());
        radiusSlider.setMin(0);
    }

    public void update() {
        AbstractMapNode node = MindMap.getInstance().getSelectedNode();
        backgroundColor.setValue(node.getBackgroundColor());
        radiusSlider.setValue(node.getRadius());
        borderColor.setValue((Color)node.getNodeShape().getStroke());
        setBorder();
    }

}
